package com.mephi.gui;

import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;
import org.apache.log4j.Logger;
import com.mephi.util.PieceColor;
import com.mephi.util.PieceLoaderKey;
import com.mephi.util.PieceSort;


/**
 * Class stored default pieces images.
 * @author GM Mephisto
 * @version 1.0
 * 2011-06-23
 */
public class PiecesLoader
{
	/**
	 * 
	 * @param piecesImagesName the name of pieces images to set
	 * @return map of pieces images or null if piecesImagesName does not fit
	 * to any of available pieces names
	 */
	public static Map<PieceLoaderKey, Piece> getPiecesMap(String piecesImagesName)
	{
		if (piecesImagesName.equals("Stauton"))
		{
			if (!BASE_DIR.endsWith("Stauton"))
			{
				BASE_DIR = System.getProperty("user.dir") + "/images/Stauton/"; 
				loadPiecesImages();
			}
			return piecesMap;
		}
			
		return null;
	}
	
	private static void loadPiecesImages()
	{
		logger.debug("BASE_DIR: " + BASE_DIR);
		//black pieces
	/*	String path = BASE_DIR+"blackPawn.png";
		logger.debug("path: " + path);
		logger.debug("key: " + PieceLoaderKey.bK);*/
		
		piecesMap.put(PieceLoaderKey.bK, 
				new Piece(
						PieceSort.KING, 
						PieceColor.BLACK, 
						new ImageIcon(BASE_DIR+"blackKing.png"))
		);
		piecesMap.put(PieceLoaderKey.bQ, 
				new Piece(
						PieceSort.QUEEN, 
						PieceColor.BLACK, 
						new ImageIcon(BASE_DIR+"blackQueen.png"))
		);
		piecesMap.put(PieceLoaderKey.bR, 
				new Piece(
						PieceSort.ROOK, 
						PieceColor.BLACK, 
						new ImageIcon(BASE_DIR+"blackRook.png"))
		);
		piecesMap.put(PieceLoaderKey.bB, 
				new Piece(
						PieceSort.BISHOP, 
						PieceColor.BLACK, 
						new ImageIcon(BASE_DIR+"blackBishop.png"))
		);
		piecesMap.put(PieceLoaderKey.bN, 
				new Piece(
						PieceSort.KNIGHT, 
						PieceColor.BLACK, 
						new ImageIcon(BASE_DIR+"blackKnight.png"))
		);
		piecesMap.put(PieceLoaderKey.bp, 
				new Piece(
						PieceSort.PAWN, 
						PieceColor.BLACK, 
						new ImageIcon(BASE_DIR+"blackPawn.png"))
		);
		
		//white pieces
		piecesMap.put(PieceLoaderKey.wK, 
				new Piece(
						PieceSort.KING, 
						PieceColor.WHITE, 
						new ImageIcon(BASE_DIR+"whiteKing.png"))
		);
		piecesMap.put(PieceLoaderKey.wQ, 
				new Piece(
						PieceSort.QUEEN, 
						PieceColor.WHITE, 
						new ImageIcon(BASE_DIR+"whiteQueen.png"))
		);
		piecesMap.put(PieceLoaderKey.wR, 
				new Piece(
						PieceSort.ROOK, 
						PieceColor.WHITE, 
						new ImageIcon(BASE_DIR+"whiteRook.png"))
		);
		piecesMap.put(PieceLoaderKey.wB, 
				new Piece(
						PieceSort.BISHOP, 
						PieceColor.WHITE, 
						new ImageIcon(BASE_DIR+"whiteBishop.png"))
		);
		//logger.debug("key: " + PieceLoaderKey.wN);
		piecesMap.put(PieceLoaderKey.wN, 
				new Piece(
						PieceSort.KNIGHT, 
						PieceColor.WHITE, 
						new ImageIcon(BASE_DIR+"whiteKnight.png"))
		);
		piecesMap.put(PieceLoaderKey.wp, 
				new Piece(
						PieceSort.PAWN, 
						PieceColor.WHITE, 
						new ImageIcon(BASE_DIR+"whitePawn.png"))
		);
	}
	
	private static Map<PieceLoaderKey, Piece> piecesMap = new HashMap<PieceLoaderKey, Piece>(12);
	private static Logger logger = Logger.getLogger("LOG");
	private static String BASE_DIR = System.getProperty("user.dir") + "/images/Stauton/"; 
	
	static
	{
		loadPiecesImages();	
	}
}
